package com.ton.utils.tron.http;

import cn.hutool.json.JSONUtil;
import com.ton.utils.http.HttpUtils;
import com.ton.utils.tron.http.tron.AccountInfo;
import com.ton.utils.tron.http.tron.BandwidthInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class TronAPI extends HttpUtils {
    String TRON_KEY = "7bbec29f-0c9f-4908-b0d4-335a1acec813";
    // request.addHeader("TRON-PRO-API-KEY", "61c7495c-eb99-46df-a1c7-e3c25aae1ece")
    public static String  get(String url, Map<String,Object> params){
        Map<String,String> headers=new HashMap<>();
        headers.put("TRON-PRO-API-KEY","1ad1922a-6b17-4215-8576-1f6d1127a883");
        return HttpUtils.get(url, headers, params);

    }

    /**
     * 根据地址获取一个账户的所有信息
     * @param address TRON地址
     * @return AccountInfo 对象
     */
    public static AccountInfo getAccountInfo(String address){
      String url = "https://apilist.tronscanapi.com/api/accountv2";
      Map<String,Object> params =new HashMap<>();
      params.put("address",address);
      String json = TronAPI.get(url, params);
      try {
          AccountInfo bean = JSONUtil.toBean(json, AccountInfo.class);
          System.out.println(bean);
          return bean;

      }catch (Exception e){
          log.error("JSON对象转换异常！！");
      }
        return new AccountInfo();
    }


    /**
     * 获取当前剩余能量
     * @param address
     * @return
     */
    public static BandwidthInfo getBandwidthInfo(String address){
        AccountInfo accountInfo = getAccountInfo(address);
        return accountInfo.getBandwidth();
    }

}
